package com.audi.view;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.activity.ActivityReviewImageCrop;

public class DeviceUpdatePhoto {
	Bitmap photo;
	Button crop;
	ImageView imagev, imagev2;
	public static int cameraCapture = 1;
	public static int picCrop = 2;
	public static int pickFromFile = 3;

	static Uri picUri;
	static int status,status2;
	//private Uri mImageCaptureUri;
	Activity activity;
	AudiDialog dialog;
	public DeviceUpdatePhoto(Activity activity) {
		this.activity = activity;

	}

	public Uri getPicUri() {
		return picUri;
	}
	public int getstatus() {
		return status;
	}
	public int getstatus2() {
		return status2;
	}
	
	@SuppressWarnings("static-access")
	public void setPicUri(Uri picUri) {
		this.picUri = picUri;
	}

	public void updatePhotoTitle() {
		dialog = new AudiDialog(activity);
		dialog.setContentView(R.layout.upload_photo_card_dialog);
		TextView titlePhoto = (TextView) dialog.findViewById(R.id.text_title_);
		TextView cam = (TextView) dialog.findViewById(R.id.text_camera);
		titlePhoto.setText(R.string.update_photo);
		status2=0;
		// CAMera pick
		cam.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				
				picUri = Uri.fromFile(new File(Environment
						.getExternalStorageDirectory(), "tmp_avatar" + ".jpg"));
				intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
						picUri);
				try {
					intent.putExtra("return-data", true);
					activity.startActivityForResult(intent, cameraCapture);
				} catch (ActivityNotFoundException e) {
					e.printStackTrace();
				}

			}
		});
		dialog.show();
	}

	// call to show dilog
	public void updatePhoto() {

		dialog = new AudiDialog(activity);
		dialog.setContentView(R.layout.upload_photo_card_dialog);
		TextView cam = (TextView) dialog.findViewById(R.id.text_camera);

		// CAMera pick
		cam.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				camphoto();			
			}
		});
		

		// Gallery pick method
		TextView galle = (TextView) dialog.findViewById(R.id.text_gallery);
		galle.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				picphoto();
			}
		});
		dialog.show();
	}
	public void picphoto(){
		Intent intent = new Intent();
		status=2;
		status2=3;
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		picUri = Uri.fromFile(new File(Environment
				.getExternalStorageDirectory(), "tmp_avatar22" + ".jpg"));
		intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
				picUri);
		try {
			activity.startActivityForResult(Intent.createChooser(
					intent, "Complete action using"), pickFromFile);
		} catch (ActivityNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();		
		}
	}
	public void camphoto(){
		status=1;
		status2=3;
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		picUri = Uri.fromFile(new File(Environment
				.getExternalStorageDirectory(), "tmp_avatar22" + ".jpg"));
		intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,picUri);
		try {
			intent.putExtra("return-data", true);
			activity.startActivityForResult(intent, cameraCapture);
		} catch (ActivityNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	// dimiss dialog
	public void dimissUpdate(){
		
		dialog.dismiss();
	}
	
	// crop image method
	public void performcrop(Uri uri) {
		try {
			// call the standard crop action intent (the user device may not
			// support it)
			//status=3;
			Intent cropIntent = new Intent("com.android.camera.action.CROP");
			// indicate image type and Uri
			cropIntent.setDataAndType(uri, "image/*");
			// set crop properties
			cropIntent.putExtra("crop", "true");
			// indicate aspect of desired crop
			cropIntent.putExtra("aspectX", 1);

			cropIntent.putExtra("aspectY", 1);
			// indicate output X and Y
			cropIntent.putExtra("outputX", 256);
			cropIntent.putExtra("outputY", 256);
			// retrieve data on return
			// cropIntent.putExtra("return-data",
			// true);
			// start the activity - we handle returning in onActivityResult

			File f = new File(Environment.getExternalStorageDirectory(),
					"/temporary_holder.jpg");
			try {
				f.createNewFile();
			} catch (IOException ex) {
				Log.e("io", ex.getMessage());
			}

			uri = Uri.fromFile(f);
			cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
			activity.startActivityForResult(cropIntent, picCrop);
			//dialog.dismiss();

		} catch (ActivityNotFoundException anfe) {
			// display an error message
			String errorMessage = "Whoops - your device doesn't support the crop action!";
			Toast.makeText(activity.getApplicationContext(), errorMessage,
					Toast.LENGTH_SHORT).show();
		}

	}

//	/* important call on the activity ver3.1-11/9/2014 Namnt16 */
//	int tempActionResult;
//	protected void onActivityResult(int requestCode, int resultCode, Intent
//			 data) {
//			 Intent myIntent = new Intent(this, ActivityReviewImageCrop.class);
//			 super.onActivityResult(requestCode, resultCode, data);
//			 if (resultCode==555) {
//					upPhoto.dimissUpdate();
//					Log.d("start crop", "dgfgfdgfdgdf");
//					tempActionResult=0;
//				 }
//			 if (resultCode == RESULT_OK) {
//			 // user is returning from capturing an image using the camera
//			 if (requestCode == upPhoto.cameraCapture) {
//			 upPhoto.performcrop(upPhoto.getPicUri());
//			 Log.d("start crop", "crop crop crop");
//			 tempActionResult=1;
//			 } 
//			 else if(requestCode == upPhoto.picCrop){
//			 myIntent.putExtra("Req", upPhoto.getstatus());
//			 //myIntent.putExtra("activity",mActivity.getApplicationContext());
//			 this.startActivityForResult(myIntent, 555);
//			 
//			 Log.d("start crop", "reviview=====-''/////");
//			 tempActionResult=0;
//			 }
//			 if (requestCode == upPhoto.pickFromFile) {
//				 tempActionResult=2;
//			 upPhoto.setPicUri(data.getData());
//			 upPhoto.performcrop(upPhoto.getPicUri());
//			 Log.d("start crop", "pick  form file=====- "+tempActionResult);	
//			 
//			 }			 
//			 }else{
//				 Log.d("close ", "======== close" +tempActionResult);
//				// Toast.makeText(activity, "sssss", Toast.LENGTH_LONG).show();
//				    if (tempActionResult == 1) {
//				    	upPhoto.camphoto();	
//					 Log.d("stop", "''/////" +tempActionResult);
//					 tempActionResult = 0;
//				    } 
//				    if(tempActionResult==2){
//				    	//upPhoto.performcrop(upPhoto.getPicUri());
//				    	upPhoto.picphoto();
//				    	Log.d("stop", "''/////" +tempActionResult);
//					tempActionResult = 0;
//					//this.finish();
//				    }
//			 }
//			 }
}
